Public Class T_L_Req_Akta
    Dim dtable As New DataTable
    Dim dadapter As New OleDb.OleDbDataAdapter
    Private t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12
    Private nw As Boolean

    Private Sub load_grid()
        dtable.Clear()
        open_con()
        dadapter = New OleDb.OleDbDataAdapter("select * from v_req_akta", DbCon)
        dadapter.Fill(dtable)
        DataGridView1.DataSource = dtable
        DataGridView1.Columns(0).Visible = False
        DataGridView1.Columns(1).Visible = False
        DataGridView1.Columns(2).Visible = False
        DataGridView1.Columns(3).Visible = False
        DataGridView1.Columns(4).Visible = False
        'DataGridView1.Columns(4).HeaderText = "Level"
        'DataGridView1.Columns(4).DefaultCellStyle.Format = "##,###"
        'DataGridView1.Columns(5).DefaultCellStyle.Format = "##,###"
        'DataGridView1.Columns(6).DefaultCellStyle.Format = "##,###"
        DataGridView1.RowHeadersVisible = False
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridView1.AutoResizeColumns()
        DataGridView1.AllowUserToAddRows = False
        DataGridView1.ReadOnly = True
        DbCon.Close()
    End Sub
    Private Sub detail(ByVal id)
        open_db("select * from v_req_akta where req_akta_id=" & id)
        While Read.Read()
            Me.TextBox1.Text = isnull(Read("no_kios"))
            Me.TextBox2.Text = isnull(Read("nama"))
            Me.TextBox3.Text = isnull(Read("no_ppjb"))
            Me.TextBox4.Text = isnull(Read("no_ppsm"))
            Me.DateTimePicker1.Text = isnull(Read("tgl_req_akta"))
            If isnull(Read("support_doc")) Then
                Me.CheckBox1.CheckState = CheckState.Indeterminate
            Else
                Me.CheckBox1.Checked = isnull(Read("support_doc"))
            End If
            If isnull(Read("cek_pembayaran")) Then
                Me.CheckBox2.CheckState = CheckState.Indeterminate
            Else
                Me.CheckBox2.Checked = isnull(Read("cek_pembayaran"))
            End If
            If isnull(Read("complete")) Then
                Me.CheckBox3.CheckState = CheckState.Indeterminate
            Else
                Me.CheckBox3.Checked = isnull(Read("complete"))
            End If
            Me.TextBox5.Text = isnull(Read("unit_id"))
            Me.TextBox6.Text = isnull(Read("tenant_id"))
            Me.TextBox7.Text = isnull(Read("ppjb_id"))
            Me.TextBox8.Text = isnull(Read("ppsm_id"))
            Select Case isnull(Read("req_akta_type"))
                Case "PPSM"
                    RadioButton1.Checked = True
                    RadioButton2.Checked = False
                Case "PPJB"
                    RadioButton1.Checked = False
                    RadioButton2.Checked = True
                Case Else
                    RadioButton1.Checked = False
                    RadioButton2.Checked = False
            End Select
        End While
        close_db()
        set_param(id)
    End Sub
    Private Sub set_param(ByVal id)
        t0 = id
        t1 = TextBox1.Text
        t2 = TextBox2.Text
        t3 = TextBox3.Text
        t4 = TextBox4.Text
        t5 = TextBox5.Text
        t6 = TextBox6.Text
        t7 = TextBox7.Text
        t8 = TextBox8.Text
        t9 = DateTimePicker1.Text
        t10 = CheckBox1.Checked
        t11 = CheckBox2.Checked
        If RadioButton1.Checked = True Then
            t12 = "PPSM"
        ElseIf RadioButton2.Checked = True Then
            t12 = "PPJB"
        Else
            t12 = ""
        End If
    End Sub
    Private Sub save()
        Dim qry As String
        Dim i As Integer
        If Not cek_null() Then
            If nw Then
                qry = "insert into l_t_req_akta (unit_id,tenant_id,ppsm_id,ppjb_id,req_akta_type," & _
                "tgl_req_akta,support_doc,cek_pembayaran"
                qry = qry + ") values ("
                qry = qry + TextBox5.Text & "," & TextBox6.Text & "," & TextBox7.Text & "," & TextBox8.Text & ","
                If RadioButton1.Checked Then
                    qry = qry + ",'PPSM',"
                End If
                If RadioButton2.Checked Then
                    qry = qry + ",'PPJB',"
                End If
                qry = qry + "'" & DateTimePicker1.Value.ToShortDateString() & "',"
                If CheckBox1.Checked Then
                    qry = qry + "1,"
                Else
                    qry = qry + "0,"
                End If
                If CheckBox2.Checked Then
                    qry = qry + "1"
                Else
                    qry = qry + "0"
                End If
                qry = qry + ")"
                nw = False
            Else
                qry = "update l_t_req_akta set "
                i = 0
                If TextBox1.Text <> t1 Then
                    qry = qry + "unit_id='" & TextBox5.Text & "'"
                    i += 1
                End If
                If TextBox2.Text <> t2 Then
                    If i > 0 Then
                        qry = qry + ","
                    End If
                    i += 1
                    qry = qry + "tenant_id=" & TextBox6.Text
                End If
                If TextBox3.Text <> t3 Then
                    If i > 0 Then
                        qry = qry + ","
                    End If
                    i += 1
                    qry = qry + "ppsm_id='" & TextBox7.Text & "'"
                End If
                If TextBox4.Text <> t4 Then
                    If i > 0 Then
                        qry = qry + ","
                    End If
                    i += 1
                    qry = qry + "ppbj_id='" & TextBox8.Text & "'"
                End If
                If DateTimePicker1.Text <> t9 Then
                    If i > 0 Then
                        qry = qry + ","
                    End If
                    i += 1
                    qry = qry + "tgl_req_akta='" & DateTimePicker1.Value.ToShortDateString & "'"
                End If
                Select Case t12
                    Case "PPSM"
                        If RadioButton2.Checked Then
                            If i > 0 Then
                                qry = qry + ","
                            End If
                            i += 1
                            qry = qry + "req_akta_type='PPJB'"
                        End If
                    Case "PPJB"
                        If RadioButton1.Checked Then
                            If i > 0 Then
                                qry = qry + ","
                            End If
                            i += 1
                            qry = qry + "req_akta_type='PPSM'"
                        End If
                End Select
                If CheckBox1.Checked <> t10 Then
                    If i > 0 Then
                        qry = qry + ","
                    End If
                    i += 1
                    If CheckBox1.Checked Then
                        qry = qry + "support_doc=1"
                    Else
                        qry = qry + "support_doc=0"
                    End If
                End If
                If CheckBox2.Checked <> t11 Then
                    If i > 0 Then
                        qry = qry + ","
                    End If
                    i += 1
                    If CheckBox2.Checked Then
                        qry = qry + "cek_pembayaran=1"
                    Else
                        qry = qry + "cek_pembayaran=0"
                    End If
                End If
                qry = qry + " where req_akta_id =" & t0
            End If
            exec_qr(qry)
            load_grid()
            set_param(t0)
        Else
            MsgBox("Harap mengisi semua data", MsgBoxStyle.Information, "Isi Data")
        End If
    End Sub

    Private Function cek_change() As Boolean
        If TextBox1.Text <> t1 Then
            Return True
        Else
            If TextBox2.Text <> t2 Then
                Return True
            Else
                If TextBox3.Text <> t3 Then
                    Return True
                Else
                    If TextBox4.Text <> t4 Then
                        Return True
                    Else
                        If TextBox5.Text <> t5 Then
                            Return True
                        Else
                            Return False
                        End If
                    End If
                End If
            End If
        End If
    End Function
    Private Function cek_null() As Boolean
        If TextBox1.Text = "" Then
            cek_null = True
        Else
            If TextBox2.Text = "" Then
                cek_null = True
            Else
                If TextBox3.Text = "" Then
                    cek_null = True
                Else
                    If TextBox4.Text = "" Then
                        cek_null = True
                    Else
                        'If TextBox5.Text = "" Then
                        '    cek_null = True
                        'Else
                        cek_null = False
                        'End If
                    End If
                End If
            End If
        End If
    End Function

    Private Sub T_L_PPSM_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        load_grid()
        nw = True
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim jwb
        nw = False
        If cek_change() Then
            jwb = MsgBox("Data telah berubah, apakah data akan di simpan?", MsgBoxStyle.YesNo, "Save Changes")
            If jwb = 6 Then
                save()
            End If
        End If
        detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
    End Sub
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        nw = True
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
        DateTimePicker1.Text = ""
        CheckBox1.Checked = False
        CheckBox2.Checked = False
        CheckBox3.Checked = False
        RadioButton1.Checked = False
        RadioButton2.Checked = False
        set_param("")
        TextBox1.Focus()
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        save()
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Dim x
        Dim sql As String
        x = MsgBox("Apakah yakin akan menghapus data?", MsgBoxStyle.YesNo, "Detele Data")
        If x = 6 Then
            sql = "delete from l_t_req_akta where req_akta_id=" & t0
            exec_qr(sql)
            load_grid()
        End If
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        DataGridView1.CurrentCell = DataGridView1.Item(5, 0)
        detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        Dim i As Integer
        If DataGridView1.CurrentRow Is Nothing Then
            i = 1
        Else
            i = DataGridView1.CurrentRow.Index
        End If
        If i - 1 >= 0 Then
            DataGridView1.CurrentCell = DataGridView1.Item(5, i - 1)
            detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
        End If
    End Sub

    Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
        Dim i As Integer
        If DataGridView1.CurrentRow Is Nothing Then
            i = -1
        Else
            i = DataGridView1.CurrentRow.Index
        End If
        If i + 1 < DataGridView1.RowCount Then
            DataGridView1.CurrentCell = DataGridView1.Item(5, i + 1)
            detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
        End If
    End Sub

    Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click
        DataGridView1.CurrentCell = DataGridView1.Item(5, DataGridView1.RowCount - 1)
        detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
    End Sub

    Private Sub ToolStripButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton8.Click
        Me.Close()
    End Sub

    Private Sub TextBox2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
        If e.KeyCode = Keys.F3 Then
            Dim frm2 As New P_Tenant
            SQL = "select * from m_tenant"
            namafrm = "Select Tenant"
            Me.TextBox2.Text = frm2.getselection
            TextBox6.Text = T_id
        End If
    End Sub

    Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.F3 Then
            Dim frm2 As New P_Tenant
            SQL = "select unit_id,no_kios,nama from vw_unit"
            namafrm = "Select Unit"
            Me.TextBox1.Text = frm2.getselection
            TextBox5.Text = T_id
        End If
    End Sub
    Private Sub DataGridView1_Keyup(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Down Then
            If DataGridView1.CurrentRow.Index + 1 < DataGridView1.RowCount Then
                detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index + 1).Value)
            End If
        End If
        If e.KeyCode = Keys.Up Then
            If DataGridView1.CurrentRow.Index - 1 >= 0 Then
                detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index - 1).Value)
            End If
        End If
    End Sub

End Class